﻿
Imports System.Xml
Imports Aras.IOM


Public Class Accueil



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim attributes As String

        If RadioButton1.Checked = True Then
            attributes = "all"
        Else
            attributes = "none"
        End If

        MakeXMI(attributes)


    End Sub

    Private Sub Accueil_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load




        reload_combobox()


    End Sub


    Private Function pass_filter(ByVal ItemTypeName As String)

        Dim match_string As String
        Dim filter_result As Boolean

        filter_result = True


        If Suff1.CheckState = CheckState.Checked Then
            filter_result = False
            match_string = Split(ItemTypeName, "_")(0)
            If match_string = FilterSuff1.Text Then
                filter_result = True
            End If
        End If

        If Suff2.CheckState = CheckState.Checked Then
            filter_result = False
            match_string = Split(ItemTypeName, "_")(0)
            If match_string = FilterSuff2.Text Then
                filter_result = True
            End If
        End If

        If Suff3.CheckState = CheckState.Checked Then
            filter_result = False
            match_string = Split(ItemTypeName, "_")(0)
            If match_string = FilterSuff3.Text Then
                filter_result = True
            End If
        End If

        Return filter_result

    End Function

    Public Sub MakeXMI(ByVal IncludeAttributes As String)


        'variables pour XMI
        Dim ClassNumber As Integer
        Dim ItemTypeName As String
        Dim UMLClass As String
        Dim ModelNumber As Integer
        Dim SubClassString As String
        Dim ConnectionString As String
        Dim ClassNode As XmlNode
        Dim XMI_NodeString As String
        Dim CountSubClass As Integer
        Dim ItemTypeClassNum As Integer
        Dim ItemClassElements As String
        Dim parent_is_relationship As Boolean
        Dim child_is_relationship As Boolean
        Dim avancement As Double
        Dim XML_UMLClass As XmlDocument
        Dim XMIenvelop As Xml.XmlDocument
        Dim conn As ConnecteurAras
        Dim itemtypes As Item
        Dim XMLClassElements As XmlDocument
        Dim PolySourceName As String
        Dim PolySourceNumber As String
        'variables techniques
        Dim NbItemTypes As Integer
        Dim i As Integer
        Dim ElementNumber As Integer
        Dim taberreur(0 To 400) As Integer
        Dim k As Integer = 0
        Dim tabproperty(0 To 50) As String
        Dim SQL_request As Item
        Dim tablename As String
        Dim AddedClassQty As Integer = 0
        'initialisation des variables
        XML_UMLClass = New XmlDocument
        XMIenvelop = New Xml.XmlDocument
        avancement = 0
        ModelNumber = 4 'ouverture du template XMI
        ElementNumber = 0

        RelNum.Text = "XXX/XXX"
        ITnum.Text = "XXX/XXX"
        DirectNum.Text = "XXX/XXX"

        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = ProgressBar1.Width
        ProgressBar1.Value = 0



        'recup types
        ProcessingText.TextAlign = HorizontalAlignment.Center
        ProcessingText.Text = "Connect To Aras"

        'Connection Aras Innovator
        Try
            conn = New ConnecteurAras(URLtext.Text, DBtext.Text, Ltext.Text, Ptext.Text)

        Catch ex As Exception
            MsgBox("Connection Failed : " & ex.Message)
            GoTo ErrorCatching
        End Try


        'Récupération du fichier template
        ProcessingText.Text = "Get XMI Template Enveloppe"
        XMIenvelop = getXMIEnveloppe()



        '''''''''''''''''''''''''
        '       ITEM TYPES      '
        '''''''''''''''''''''''''


        'récupération des itemtypes (is_relationship='0')
        ProcessingText.Text = "Get Itemtypes AML from Aras Innovator"
        itemtypes = conn.getitemtypes(False, True)
        NbItemTypes = itemtypes.getItemCount

        ITnum.Text = "000/" & CStr(NbItemTypes)
        'créer tableau avec tous les éléments à traiter
        For i = 0 To (NbItemTypes - 1)
            taberreur(i) = i
        Next

        'créer l'item UML:Class pour chaque ItemType
        ProcessingText.Text = "Create UML:Class Nodes"
        avancement = ProgressBar1.Value
        Dim ItemtypeChildNumber As Integer

        ReDim Preserve taberreur(NbItemTypes - 1)
        'on re-parcours les éléments non validés tant qu'il y en a
        While (taberreur.Length) > 0
            ItemtypeChildNumber = 0
            k = 0
            Dim counterreur = UBound(taberreur)
            Dim taberreur2(0 To 400) As Integer
            Dim j As Integer
            Dim l As Integer

            For j = 0 To counterreur

                i = taberreur(j)

                avancement = avancement + (20 / NbItemTypes)
                Try

                    ProgressBar1.Value = CInt(avancement)

                Catch ex As Exception

                    ProgressBar1.Value = ProgressBar1.Maximum

                End Try

                ElementNumber = ElementNumber + 1

                'set attribute values
                ClassNumber = i
                ItemTypeName = itemtypes.getItemByIndex(i).getProperty("name")



                '------------------------------------------------------------------------'
                'Run if not blocked by Prefix Filter
                '------------------------------------------------------------------------'
                If pass_filter(ItemTypeName) Then

                    '--------------------------------------------------------------------------------------------------------------'
                    '--------------------------------------------------------------------------------------------------------------'
                    '                               Gestion de la recherche des attributs
                    '--------------------------------------------------------------------------------------------------------------'
                    '--------------------------------------------------------------------------------------------------------------'

                    If IncludeAttributes = "all" Then
                        tablename = ""

                        tablename = itemtypes.getItemByIndex(i).getProperty("instance_data")
                        SQL_request = conn.getItemProperties(tablename)

                        Try
                            ItemtypeChildNumber = SQL_request.nodeList.Count
                            ReDim tabproperty(ItemtypeChildNumber - 1)
                            For l = 0 To ItemtypeChildNumber - 1
                                tabproperty(l) = SQL_request.nodeList.Item(l).ChildNodes.Item(3).InnerText
                            Next

                        Catch ex As Exception

                            'erreur dans recherche sql - donc recherche classique
                            ItemtypeChildNumber = itemtypes.getItemByIndex(i).node.ChildNodes.Count
                            ReDim tabproperty(ItemtypeChildNumber - 1)
                            For l = 0 To ItemtypeChildNumber - 1
                                tabproperty(l) = itemtypes.getItemByIndex(i).node.ChildNodes.Item(l).LocalName
                            Next

                        End Try



                    ElseIf IncludeAttributes = "list" Then

                        tablename = itemtypes.getItemByIndex(i).getProperty("instance_data")
                        SQL_request = conn.getItemProperties(tablename)

                        Try
                            ItemtypeChildNumber = SQL_request.nodeList.Count
                            ReDim tabproperty(ItemtypeChildNumber - 1)
                            For l = 0 To ItemtypeChildNumber - 1
                                tabproperty(l) = SQL_request.nodeList.Item(l).ChildNodes.Item(3).InnerText
                            Next

                        Catch ex As Exception

                            'erreur dans recherche sql - donc recherche classique
                            ItemtypeChildNumber = itemtypes.getItemByIndex(i).node.ChildNodes.Count
                            ReDim tabproperty(ItemtypeChildNumber - 1)
                            For l = 0 To ItemtypeChildNumber - 1
                                tabproperty(l) = itemtypes.getItemByIndex(i).node.ChildNodes.Item(l).LocalName
                            Next

                        End Try



                    End If
                    '--------------------------------------------------------------------------------------------------------------'




                    'recherche de subclass
                    XMLClassElements = New XmlDocument
                    ItemTypeClassNum = ElementNumber
                    SubClassString = ""

                    '''''''''''''''''''''''''''''
                    '       POLY SOURCES        '
                    '''''''''''''''''''''''''''''

                    Dim polysourcecount As Integer = 0


                    Dim n As Integer = 0

                    If itemtypes.getItemByIndex(i).getProperty("implementation_type") = "polymorphic" Then
                        ' look for polysources
                        'MsgBox(itemtypes.getItemByIndex(i).getProperty("name"))
                        polysourcecount = itemtypes.getItemByIndex(i).getRelationships("Morphae").nodeList.Count
                        For n = 0 To polysourcecount - 1
                            PolySourceName = itemtypes.getItemByIndex(i).getRelationships("Morphae").getItemByIndex(n).getRelatedItem.getProperty("name")

                            'rechercher si parent existe
                            PolySourceNumber = XMI.getXMIClassNumber(XMIenvelop, PolySourceName)
                            If PolySourceNumber <> 0 Then
                                'créer le lien
                                'ajout du noeud de l'item Generalization
                                UMLClass = XMI.MakeXMI_UML_Generalization(ElementNumber, ModelNumber, PolySourceNumber, ItemTypeClassNum)
                                ElementNumber = ElementNumber + 1

                                'MAJ du fichier XMI
                                XMIenvelop = XMI.AddNodeToTemplate(UMLClass, XMIenvelop)

                                'construction de la chaine de sous-classe
                                SubClassString += "UMLGeneralization." & ElementNumber & " "
                            Else
                                'keep as on hold only if passes the filter
                                If pass_filter(PolySourceName) Then
                                    taberreur2(k) = i
                                    k = k + 1
                                    GoTo ItemSuivant
                                End If
                            End If
                        Next

                    End If


                    ClassNum.Text = CStr(ElementNumber)


                    '''''''''''''''''''''''''''''
                    '       SPECIFIC CLASS      '
                    '''''''''''''''''''''''''''''

                    'récupération des specifiques class (is_relationship='0')
                    ItemClassElements = itemtypes.getItemByIndex(i).getProperty("class_structure")

                    If ItemClassElements IsNot Nothing Then
                        XMLClassElements.LoadXml(ItemClassElements)

                        'calcul le nombre de node
                        CountSubClass = XMLClassElements.DocumentElement.ChildNodes.Count
                        ItemTypeClassNum = ItemTypeClassNum + 2 * CountSubClass

                        For Each ClassNode In XMLClassElements.DocumentElement.ChildNodes

                            'ajout du noeud de la sous-classe
                            UMLClass = XMI.MakeXMI_UML_SubClass(ElementNumber, (ClassNode.Attributes(0).Value), ModelNumber, (ElementNumber + 1))
                            ElementNumber = ElementNumber + 1

                            'MAJ du fichier XMI
                            XMIenvelop = XMI.AddNodeToTemplate(UMLClass, XMIenvelop)

                            'ajout du noeud de l'item Generalization
                            UMLClass = XMI.MakeXMI_UML_Generalization(ElementNumber, ModelNumber, (ElementNumber - 1), ItemTypeClassNum)
                            ElementNumber = ElementNumber + 1

                            'MAJ du fichier XMI
                            XMIenvelop = XMI.AddNodeToTemplate(UMLClass, XMIenvelop)

                            'construction de la chaine de sous-classe
                            SubClassString += "UMLGeneralization." & ElementNumber & " "
                        Next

                    End If
                    'recherche de connexions
                    ConnectionString = ""





                    '''''''''''''''''''''''''''''
                    '            CLASS          '
                    '''''''''''''''''''''''''''''

                    'conversion des itemtypes en UML:Class
                    'création de l'élément UML:Class + ajout de l'élément XMInode  ici --> UML:Class
                    XMI_NodeString = XMI.MakeXMI_UML_Class(ItemTypeClassNum, ItemTypeName, ModelNumber, SubClassString, ConnectionString, IncludeAttributes, tabproperty)
                    XMIenvelop = XMI.AddNodeToTemplate(XMI_NodeString, XMIenvelop)
                    AddedClassQty = AddedClassQty + 1
                    ITnum.Text = CStr(AddedClassQty) & "/" & CStr(NbItemTypes)

                End If

ItemSuivant:

            Next
            taberreur = taberreur2
            ReDim Preserve taberreur(k - 1)


        End While


        '''''''''''''''''''''''''''''''''
        '       RELATIONSHIP TYPES      '
        '''''''''''''''''''''''''''''''''

        'récupération des RelationshipTypes (is_relationship='1')
        Dim Relationshiptypes As Item
        Dim ChildName As String
        Dim ParentName As String
        Dim ChildNumber As Integer
        Dim RelChildNumber As Integer
        Dim ParentNumber, RelParentNumber As Integer
        Dim RelationshipName As String
        k = 0
        ElementNumber = ElementNumber + 1

        ProcessingText.Text = "Add Relationships Nodes"
        itemtypes = conn.getitemtypes(True, False)
        Relationshiptypes = conn.getRelationshiptypes()
        NbItemTypes = Relationshiptypes.getItemCount

        AddedClassQty = 0
        RelNum.Text = "000/" & CStr(NbItemTypes)
        ReDim taberreur(400)
        'créer tableau avec tous les éléments à traiter
        For i = 0 To (NbItemTypes - 1)
            taberreur(i) = i
        Next


        ReDim Preserve taberreur(NbItemTypes - 1)
        'on reparcour les éléments non validés tant qu'il y en a
        While (taberreur.Length) > 0
            k = 0
            Dim counterreur = UBound(taberreur)
            Dim taberreur2(0 To 400) As Integer
            Dim j As Integer

            For j = 0 To counterreur

                ChildNumber = 0
                ParentNumber = 0
                RelParentNumber = 0
                RelationshipName = ""

                i = taberreur(j)

                ProgressBar1.Value = ProgressBar1.Value + (ProgressBar1.Maximum - ProgressBar1.Value) / 3

                RelationshipName = Relationshiptypes.getItemByIndex(i).getProperty("name")

                '---- debug ----
                If RelationshipName = "Project Team" Then
                    'MsgBox("ee")
                End If
                '---- debug ----
                '--------------------------------------------------------------------------------------------------------------'
                '--------------------------------------------------------------------------------------------------------------'
                '                               Gestion de la recherche des attributs
                '--------------------------------------------------------------------------------------------------------------'
                '--------------------------------------------------------------------------------------------------------------'

                If pass_filter(RelationshipName) Then
                    If IncludeAttributes = "all" Then

                        tablename = itemtypes.getItemsByXPath(itemtypes.XPathResultItem & "[name='" & RelationshipName & "']").getProperty("instance_data")
                        SQL_request = conn.getItemProperties(tablename)

                        Try
                            ItemtypeChildNumber = SQL_request.nodeList.Count
                            ReDim tabproperty(ItemtypeChildNumber - 1)
                            For l = 0 To ItemtypeChildNumber - 1
                                tabproperty(l) = SQL_request.nodeList.Item(l).ChildNodes.Item(3).InnerText
                            Next

                        Catch ex As Exception

                            'erreur dans recherche sql - donc recherche classique
                            ItemtypeChildNumber = itemtypes.getItemByIndex(i).node.ChildNodes.Count
                            ReDim tabproperty(ItemtypeChildNumber - 1)
                            For l = 0 To ItemtypeChildNumber - 1
                                tabproperty(l) = itemtypes.getItemByIndex(i).node.ChildNodes.Item(l).LocalName
                            Next

                        End Try



                    ElseIf IncludeAttributes = "list" Then

                        tablename = itemtypes.getItemByIndex(i).getProperty("instance_data")
                        SQL_request = conn.getItemProperties(tablename)

                        Try
                            ItemtypeChildNumber = SQL_request.nodeList.Count
                            ReDim tabproperty(ItemtypeChildNumber - 1)
                            For l = 0 To ItemtypeChildNumber - 1
                                tabproperty(l) = SQL_request.nodeList.Item(l).ChildNodes.Item(3).InnerText
                            Next

                        Catch ex As Exception

                            'erreur dans recherche sql - donc recherche classique
                            ItemtypeChildNumber = itemtypes.getItemByIndex(i).node.ChildNodes.Count
                            ReDim tabproperty(ItemtypeChildNumber - 1)
                            For l = 0 To ItemtypeChildNumber - 1
                                tabproperty(l) = itemtypes.getItemByIndex(i).node.ChildNodes.Item(l).LocalName
                            Next

                        End Try



                    End If
                    '--------------------------------------------------------------------------------------------------------------'



                    ' --- retrouver la référence du parent --- '
                    ' récupération du nom de l'item parent
                    ParentName = Relationshiptypes.getItemByIndex(i).getPropertyAttribute("source_id", "name")
                    If ParentName IsNot Nothing Then
                        ' récupération du numero de classXMI du parent si déjà inscrit
                        ParentNumber = XMI.getXMIClassNumber(XMIenvelop, ParentName)
                        ' récupération du nom de l'item parent si déjà existant et si ce parent est de type is_relationship
                        RelParentNumber = XMI.getXMIAssociationClassNumber(XMIenvelop, ParentName)
                    End If
                    ' ---------------------------------------- '


                    If ParentNumber = 0 And RelParentNumber = 0 Then ' si aucun parent n'est déjà intégré dans le XMI

                        If ((Relationshiptypes.getItemByIndex(i).getRelatedItem IsNot Nothing)) And (ParentName Is Nothing) Then ' si l'item a un élément fils

                            'retrouver la référence du child
                            ChildName = Relationshiptypes.getItemByIndex(i).getRelatedItem.getProperty("name")
                            ChildNumber = XMI.getXMIClassNumber(XMIenvelop, ChildName)

                            If pass_filter(ChildName) Then
                                'création de l'UML:association
                                ParentNumber = ElementNumber + 3 'numero du child créé juste après 
                                'inversion child number et parent number
                                XMI_NodeString = XMI.MakeXMI_UML_Association(ElementNumber, ModelNumber, ParentNumber, ChildNumber, False)
                                XMIenvelop = XMI.AddNodeToTemplate(XMI_NodeString, XMIenvelop)
                                ElementNumber = ElementNumber + 3

                                'création de l' UML:class
                                SubClassString = ""
                                ConnectionString = ""
                                XMI_NodeString = XMI.MakeXMI_UML_Class(ElementNumber, RelationshipName, ModelNumber, SubClassString, ConnectionString, IncludeAttributes, tabproperty)
                                XMIenvelop = XMI.AddNodeToTemplate(XMI_NodeString, XMIenvelop)
                                ElementNumber = ElementNumber + 1
                                'affichage
                                AddedClassQty = AddedClassQty + 1
                                RelNum.Text = CStr(AddedClassQty) & "/" & CStr(NbItemTypes)

                            End If
                        Else

                            'dans ce cas on reporte le traitement de cet item à plus tard

                            taberreur2(k) = i
                            k = k + 1

                        End If

                    Else
                        'test si related_id
                        If ParentNumber = 0 Then
                            parent_is_relationship = True
                        End If

                        'si related_id existe --> créer Association Class
                        If Relationshiptypes.getItemByIndex(i).getRelatedItem IsNot Nothing Then


                            'retrouver la référence du child
                            ChildName = Relationshiptypes.getItemByIndex(i).getRelatedItem.getProperty("name")
                            ChildNumber = XMI.getXMIClassNumber(XMIenvelop, ChildName)
                            RelChildNumber = XMI.getXMIAssociationClassNumber(XMIenvelop, ChildName)

                            If ChildNumber = 0 And RelChildNumber = 0 Then
                                If pass_filter(ChildName) Then
                                    taberreur2(k) = i
                                    k = k + 1
                                End If
                            Else
                                child_is_relationship = False
                                If ChildNumber = 0 Then
                                    child_is_relationship = True
                                    ChildNumber = RelChildNumber
                                End If

                                If parent_is_relationship = True Then
                                    ParentNumber = RelParentNumber
                                End If
                                'creation des UML:AssociationClass
                                'création de l'élément UML:Class + ajout de l'élément XMInode  ici --> UML:Class
                                XMI_NodeString = XMI.MakeXMI_UML_AssociationClass(ElementNumber, RelationshipName, ModelNumber, _
                                                                                  ChildNumber, ParentNumber, _
                                                                                  parent_is_relationship, child_is_relationship, IncludeAttributes, tabproperty)
                                XMIenvelop = XMI.AddNodeToTemplate(XMI_NodeString, XMIenvelop)
                                ElementNumber = ElementNumber + 3
                            End If


                        Else
                            'si related_id n'existe pas --> créer Class + Association
                            If parent_is_relationship = True Then
                                ParentNumber = RelParentNumber
                            End If
                            'création de l'UML:association
                            ChildNumber = ElementNumber + 3 'numero du child créé juste après 
                            XMI_NodeString = XMI.MakeXMI_UML_Association(ElementNumber, ModelNumber, ChildNumber, ParentNumber, parent_is_relationship)
                            XMIenvelop = XMI.AddNodeToTemplate(XMI_NodeString, XMIenvelop)
                            ElementNumber = ElementNumber + 3

                            'création de l' UML:class
                            SubClassString = ""
                            ConnectionString = ""
                            XMI_NodeString = XMI.MakeXMI_UML_Class(ElementNumber, RelationshipName, ModelNumber, SubClassString, ConnectionString, IncludeAttributes, tabproperty)
                            XMIenvelop = XMI.AddNodeToTemplate(XMI_NodeString, XMIenvelop)
                            ElementNumber = ElementNumber + 1
                            'affichage
                            AddedClassQty = AddedClassQty + 1
                            RelNum.Text = CStr(AddedClassQty) & "/" & CStr(NbItemTypes)
                        End If


                    End If
                    parent_is_relationship = False
                End If
            Next
            ClassNum.Text = CStr(ElementNumber)
            taberreur = taberreur2
            ReDim Preserve taberreur(k - 1)


        End While




        If CheckBox1.CheckState = CheckState.Checked Then


            ''''''''''''''''''''''''''''''
            '       One to One Link      '
            ''''''''''''''''''''''''''''''
            Dim connections(20, 2) As String
            Dim OneToOneMultiNumber As Integer
            Dim progressionbar, progressStep As Double

            progressionbar = ProgressBar1.Maximum - ProgressBar1.Value

            OneToOneMultiNumber = 1


            ProcessingText.Text = "Create Direct Links"
            itemtypes = conn.getitemtypes(False, True)
            NbItemTypes = itemtypes.getItemCount

            progressStep = progressionbar / NbItemTypes

            DirectNum.Text = "000/" & CStr(NbItemTypes)
            'boucler pour tous les itemtypes de type is_relationship=false
            For i = 0 To NbItemTypes - 1

                ItemTypeName = itemtypes.getItemByIndex(i).getProperty("name")
                'début debug
                If i = 59 Then
                    i = i
                End If
                'fin debug
                tablename = itemtypes.getItemByIndex(i).getProperty("instance_data")
                connections = conn.getPropertiesLinkOfTypeItem(tablename)
                ParentNumber = XMI.getXMIClassNumber(XMIenvelop, ItemTypeName)

                If pass_filter(ItemTypeName) Then


                    'boucler pour chaque élément à rattacher
                    For j = 0 To connections.Length / 2 - 1
                        If pass_filter(connections(j, 0)) Then
                            If connections(j, 0) <> "Erreur" Then
                                ElementNumber = ElementNumber + 1
                                ChildNumber = XMI.getXMIClassNumber(XMIenvelop, connections(j, 0))
                               
                                XMI_NodeString = XMI.MakeXMI_UML_DirectedOneToOneConnection(ElementNumber, ModelNumber, ParentNumber, ChildNumber, "", OneToOneMultiNumber, False, connections(j, 1))
                                XMIenvelop = XMI.AddNodeToTemplate(XMI_NodeString, XMIenvelop)
                                OneToOneMultiNumber = OneToOneMultiNumber + 4

                            Else
                                ElementNumber = ElementNumber
                            End If
                        End If
                    Next

                    DirectNum.Text = CStr(i + 1) & "/" & CStr(NbItemTypes)
                    ClassNum.Text = CStr(ElementNumber)


                    progressionbar = progressionbar + progressStep
                    ProgressBar1.Value = CInt(progressionbar + ProgressBar1.Value)
                End If
            Next

            ProgressBar1.Value = ProgressBar1.Maximum
            ProcessingText.Text = "File completed"

        End If



        '''''''''''''''''''''''''
        '       INTERFACES      '
        '''''''''''''''''''''''''
        'récupération des interfaces (is_relationship='0')
        'itemtypes = conn.GetInterfaces()




        '''''''''''''''''''''''''
        '       RESULTAT        '
        '''''''''''''''''''''''''
        'conversion des itemtypes en UML.Class

        Dim filepath As String
        Dim FileDB As New SaveFileDialog
        FileDB.FileName = ("ResultFile.xmi")

        If (FileDB.ShowDialog() = DialogResult.OK) Then
            filepath = FileDB.FileName
        Else
            MsgBox("User selected Cancel")
            GoTo ErrorCatching
        End If



        'MAJ du fichier XMI
        XMIenvelop.Save(filepath)
        ProcessingText.Text = "File Saved in: " & filepath
        GoTo Endsub

ErrorCatching:


Endsub:

        'Déchargement des variables
        XML_UMLClass = Nothing
        XMIenvelop = Nothing
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBar1.Value = ProgressBar1.Value + 1
    End Sub

    Private Sub CheckedListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'get package list
    End Sub

    Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click
        'open CHM file
        Process.Start(System.IO.Path.Combine(Application.StartupPath, "aras2uml.chm"))
    End Sub

    Private Sub ContactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContactToolStripMenuItem.Click
        'open business card
        Dialog1.Show()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'save the profile
        Dim nodestring As String
        Dim XMLenvelop As Xml.XmlDocument
        XMLenvelop = getProfilEnveloppe()

        nodestring = "<Profil name='" & Me.TextBox1.Text & "'>" & _
                      "<URL>" & Me.URLtext.Text & "</URL>" & _
                      "<DB>" & Me.DBtext.Text & "</DB>" & _
                      "<Login>" & Me.Ltext.Text & "</Login>" & _
                      "<PW>" & Me.Ptext.Text & "</PW>" & _
                      "</Profil>"

        XMLenvelop = AddProfile(nodestring, XMLenvelop)
        XMLenvelop.Save("Profils.xml")
        reload_combobox()

    End Sub

    Public Function getProfilEnveloppe() As Xml.XmlDocument
        Dim XMIEnveloppe As Xml.XmlDocument = New Xml.XmlDocument

        XMIEnveloppe.Load("Profils.xml")

        Return XMIEnveloppe

    End Function

    Public Function AddProfile(ByVal NodeString As String, ByVal XMIenvelop As Xml.XmlDocument) As Xml.XmlDocument



        ' Ajout de l'élément créé à l'enveloppe
        Dim ItemTypesN As Xml.XmlDocumentFragment

        ItemTypesN = XMIenvelop.CreateDocumentFragment
        ItemTypesN.InnerXml = NodeString
        XMIenvelop.GetElementsByTagName("Profiles").Item(0).AppendChild(ItemTypesN)


        Return XMIenvelop

    End Function

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        'recupérer les propriétés de l'élément selectionné
        Dim Del_Element As String
        Del_Element = ComboBox1.Text

        'remplir liste des profils
        Dim XMLprofils As Xml.XmlDocument
        XMLprofils = New Xml.XmlDocument

        XMLprofils.Load("Profils.xml")
        Dim listenoeuds As Xml.XmlNodeList
        Dim noeuds As Xml.XmlNode
        listenoeuds = XMLprofils.ChildNodes.Item(0).ChildNodes
        For Each noeuds In listenoeuds
            If noeuds.Attributes("name").Value = Del_Element Then
                Me.URLtext.Text = noeuds.ChildNodes.Item(0).InnerText
                Me.DBtext.Text = noeuds.ChildNodes.Item(1).InnerText
                Me.Ltext.Text = noeuds.ChildNodes.Item(2).InnerText
                Me.Ptext.Text = noeuds.ChildNodes.Item(3).InnerText
            End If
        Next

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        'supprimer l'élément selectionné
        Dim Del_Element As String
        Del_Element = ComboBox1.Text

        If Del_Element = "Blank" Then
            MsgBox("Blank Profile can't be deleted")
        Else
            'remplir liste des profils
            Dim XMLprofils As Xml.XmlDocument
            XMLprofils = New Xml.XmlDocument

            XMLprofils.Load("Profils.xml")
            Dim listenoeuds As Xml.XmlNodeList
            Dim noeuds As Xml.XmlNode
            listenoeuds = XMLprofils.ChildNodes.Item(0).ChildNodes
            For Each noeuds In listenoeuds
                If noeuds.Attributes("name").InnerText = Del_Element Then
                    XMLprofils.ChildNodes.Item(0).RemoveChild(noeuds)
                End If
            Next

            XMLprofils.Save("Profils.xml")
            reload_combobox()
        End If




    End Sub

    Private Sub reload_combobox()
        'remplir liste des profils
        Dim XMLprofils As Xml.XmlDocument
        XMLprofils = New Xml.XmlDocument

        ComboBox1.Items.Clear()

        XMLprofils.Load("Profils.xml")
        Dim listenoeuds As Xml.XmlNodeList
        Dim noeuds As Xml.XmlNode
        listenoeuds = XMLprofils.ChildNodes.Item(0).ChildNodes
        For Each noeuds In listenoeuds
            ComboBox1.Items.Add(noeuds.Attributes("name").Value)
        Next

        ComboBox1.Text = ComboBox1.Items(0)

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.CheckState = CheckState.Checked Then
            DirectNum.Visible = True
            Label_direct.Visible = True
        Else
            DirectNum.Visible = False
            Label_direct.Visible = False
        End If
    End Sub

    Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click

    End Sub
End Class
